package collection;

import exception.CollectionEmptyException;

public class ListStack<E> implements IStack<E>{

	private SLinkedList<E> internalList = new SLinkedList<E>();
	
	@Override
	public boolean isEmpty() 
	{
		return this.internalList.isEmpty();
	}

	@Override
	public int size() 
	{
		return this.internalList.size();
	}

	@Override
	public void push(E element) 
	{
		this.internalList.addFirst(this.internalList.createNode(element));
	}

	@Override
	public E pop() throws CollectionEmptyException 
	{
		if (isEmpty())
		{
			throw new CollectionEmptyException("Stack");
		}
		else
		{
			E retVal = this.internalList.getFirst().getElement();
			this.internalList.removeFirst();
			return retVal;
		}
	}

	@Override
	public E top() 
	{
		if (isEmpty())
		{
			throw new CollectionEmptyException("Stack");
		}
		else
		{
			return this.internalList.getFirst().getElement();
		}
	}

	@Override
	public String toString()
	{		
		SLinkedList<E> reversedList = new SLinkedList<E>(this.internalList);
		reversedList.reverse();
		return reversedList.toString();
	}
	
}
